#include "dialog.h"
#include "ui_dialog.h"
#include "easyinput.h"
#include "QTime"
#include <QDebug>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    easyInput *eInput =new easyInput(this);//输入法实例化
    eInput->installEventFilter(this);//安装事件过滤器
    this->setWindowFlags(Qt::FramelessWindowHint);
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    ui->dateTimeEdit_2->setDateTime(QDateTime::currentDateTime());
}

Dialog::~Dialog()
{
    this->writeflag = 0;
    delete ui;
}

void Dialog::SetAllData(QString CarID, QString time1, QString time2, QString mask, QString tmp, QString Price,int row1)
{
    ui->lineEdit->setText(CarID);
    qDebug()<<"Set all data:" +time1 <<endl;
    qDebug()<<"Set all data:" +time2 <<endl;
    ui->dateTimeEdit->setDateTime(QDateTime::fromString(time1,"yyyy-MM-dd hh:mm:ss"));
    ui->dateTimeEdit_2->setDateTime(QDateTime::fromString(time2,"yyyy-MM-dd hh:mm:ss"));
    if(mask == "有口罩")
        ui->comboBox->setCurrentIndex(1);
    ui->doubleSpinBox->setValue(tmp.toDouble());
    if(Price == "已缴费")
        ui->comboBox_2->setCurrentIndex(1);
    this->writeflag = 1;
    this->row = row1;
    qDebug()<<this->row <<endl;
}

void Dialog::on_pushButton_clicked()
{
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}

void Dialog::on_pushButton_2_clicked()
{
    ui->dateTimeEdit_2->setDateTime(QDateTime::currentDateTime());
}

void Dialog::on_pushButton_3_clicked()
{
    unsigned int time_sec = ui->dateTimeEdit_2->dateTime().toTime_t();
    time_sec += 3600;
    QDateTime newtime =  QDateTime::fromTime_t(time_sec);
    ui->dateTimeEdit_2->setDateTime(newtime);
}

void Dialog::on_pushButton_4_clicked()
{
    unsigned int time_sec = ui->dateTimeEdit_2->dateTime().toTime_t();
    time_sec -=3600;
    QDateTime newtime =  QDateTime::fromTime_t(time_sec);
    ui->dateTimeEdit_2->setDateTime(newtime);
}

void Dialog::on_pushButton_5_clicked()
{
    QString carID = ui->lineEdit->text();
    QString inputdetetime = ui->dateTimeEdit->text();
    QString outputdatetime = ui->dateTimeEdit_2->text();
    QString tmp = ui->doubleSpinBox->text();
    QString mask = ui->comboBox->currentText();
    QString Price = ui->comboBox_2->currentText();

    QSqlQuery query;
    QString sql;
    if(!carID.isEmpty()){
        if(writeflag){
            sql = QString("update carsql set carID = '%1', time1 = '%2',time2 = '%3',Mask = '%4', tmp = '%5',flag = '%6' where rowid in(select rowid from carsql limit 1 offset %7)")
                    .arg(carID).arg(inputdetetime).arg(outputdatetime).arg(mask).arg(tmp).arg(Price).arg(this->row);

        }
        else{
            sql = QString("insert into carsql values ('1','%1','%2','%3','%4','%5','%6');").arg(carID).arg(inputdetetime)
                   .arg(outputdatetime).arg(mask).arg(tmp).arg(Price);
        }

    }
    else{
        qDebug() << " carID:none " <<endl;
    }
    qDebug() << sql <<endl;
    if(query.exec(sql)){
        qDebug() << " insert:OK!" <<endl;
    }
    else{
        qDebug() << " insert:error!" <<endl;
    }

}
